package com.lagou.handler;

import com.lagou.service.RpcRequest;
import com.lagou.service.UserServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class UserServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        // 判断是否符合约定，符合则调用本地方法，返回数据
        // msg:  UserService#sayHello#are you ok?
//        if(msg.toString().startsWith("UserService")){

        if(msg instanceof RpcRequest){
            RpcRequest rpcRequest = (RpcRequest) msg;
            if (rpcRequest.getClassName().equals("UserService")&&rpcRequest.getMethodName().equals("sayHello")){
                UserServiceImpl userService = new UserServiceImpl();

                String result = userService.sayHello(rpcRequest.getParameters()[0].toString());
                rpcRequest.setResult(result);
                ctx.writeAndFlush(rpcRequest);
            }

        }else {
            System.out.println("解析出错!");
            ctx.writeAndFlush("false");
        }


    }
}
